package com.ruitu.arad.api.okhttp;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * zhoufeng 2017/8/30 9:59
 * com.paykee_huina_wallet.activity.utils.PrefUtil
 * SharedPreferences工具类
 */
public class PrefUtil {
    /**
     * 极光推送id
     */
    public static final String KEY_JPUSH_REGISTRATION_ID = "jpush_registration_id";

    /**
     * 配置文件名称
     */
    private static final String PREFERENCE_NAME = "hcf_pref";

    /**
     * 登录id
     */
    public static final String LOGIN_ID = "login_id";

    /**
     * 第一次执行
     */
    public static final String FIRST_ACTIVITY = "firstActivity";

    /**
     * 是否同意隐私协议
     */
    public static final String IS_AGREE_PRIVACY = "isagreeprivacy";

    /**
     * Longitude
     */
    public static final String LONGITUDE = "longitude";

    /**
     * compay_name
     */
    public static final String COMPAY_NAME = "compay_name";
    /**
     * address
     */
    public static final String ADDRESS = "address";
    /**
     * aoi
     */
    public static final String AOI = "aoi";
    /**
     * poi
     */
    public static final String POI = "poi";
    /**
     * Latitude
     */
    public static final String LATITUDE = "latitude";

    /**
     * OSS secretkey
     */
    public static final String OSS_SECRETKEY = "oss_secretkey";

    /**
     * OSS secretid
     */
    public static final String OSS_SECRETID = "oss_secretid";

    /**
     * OSS endpoint
     */
    public static final String OSS_ENDPOINT = "oss_endpoint";
    /**
     * OSS domain
     */
    public static final String OSS_DOMAIN = "oss_domain";

    /**
     * OSS bucket
     */
    public static final String OSS_BUCKET = "oss_bucket";


    /**
     * 服务电话
     */
    public static final String SERVER_MOBILE = "0539-6375678";

    /**
     * 用户信息
     */
    public static final String USER_INFO_JSON = "user_info_json";

    /**
     * 用户手机号
     */
    public static final String USER_MOBILE = "user_mobile";

    /**
     *
     */
    public static final String LOGIN_TOKEN = "login_token";

    /**
     *
     */
    public static final String COUNT_NUM = "count_num";

    /**
     * 程序是否已启动
     */
    public static final String APP_HAS_STARTED = "app_has_started";

    public static boolean put(Context context, String key, Object object) {
        String type = object.getClass().getSimpleName();
        SharedPreferences.Editor editor = getEditor(context);

        if ("String".equals(type)) {
            editor.putString(key, (String) object);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) object);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) object);
        }

        return editor.commit();
    }

    public static Object get(Context context, String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = getSharedPreferences(context);

        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    public static boolean remove(Context context, String key) {
        SharedPreferences.Editor editor = getEditor(context);
        return editor.remove(key).commit();
    }

    public static boolean clear(Context context) {
        SharedPreferences.Editor editor = getEditor(context);
        return editor.clear().commit();
    }

    private static SharedPreferences.Editor getEditor(Context context) {
        return getSharedPreferences(context).edit();
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

}
